<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class mgame extends MY_Model {

	var $theme_data = array(
		'layout_main' => 'layout',
		'left' => 'include/leftcp',
	);
	public $mytable = "game";
	public $primaryKey = "game_id";
	public $resCode = "GAME";

	function __construct() {
		parent::__construct();
	}

	function field() {
		$fieldData = array();
		$fieldData['user_id'] = 0;
		$fieldData['game_title'] = '';
		$fieldData['game_code'] = '';
		$fieldData['game_time_create'] = time();
		$fieldData['game_time_update'] = time();
		$fieldData['game_content'] = '';
		$fieldData['game_status'] = 1;
		$fieldData['game_view'] = 0;
		$fieldData['game_thumb'] = '';
		$fieldData['game_thumb_large'] = '';
		$fieldData['game_file'] = '';
		$fieldData['seo_h1'] = '';
		$fieldData['seo_description'] = '';
		$fieldData['seo_keyword'] = '';
		$fieldData['seo_title'] = '';
		$fieldData['game_file_rate_height_width'] = 1;
		$fieldData['game_min_scores'] = 0;
		$fieldData['game_key'] = 0;
		$fieldData['use_id_create'] = 0;
		return $fieldData;
	}
	
	function get_by_cate($cate_id) {
		$this->db
			->from($this->mytable)
			->join('game_category as mc', 'game.game_id = mc.game_id');
		if (is_array($cate_id)){
			$this->db
				->where_in('mc.cat_id',$cate_id);
		}else {
			$this->db
				->where('mc.cat_id = '.$cate_id);
		}
		return $this->db->get()->result();
	}
	
	function update_view($game_id = 0){
		//update view
        $this->db->where("game_id", $game_id);
        $this->db->set('game_view', 'game_view+1', false);
        $this->db->update($this->mytable, array());
	}
	/*
	$permission_code:
		'post_game', 'test_game', 'process_game',
	*/
	function permission($permission_code, $admin_id = 0){
		$flag = 0;
		switch($permission_code){
			case 'post_game': 
				$flag = ($this->permission->check("insert", $this->resCode) || $this->permission->check("update", $this->resCode));
			break;
			case 'test_game': 
				$flag = $this->permission->check("select", $this->resCode);
			break;
			case 'process_game': 
				$flag = $this->permission->check("full", $this->resCode);
			break;
		}
		return $flag;
	}
}

?>